export default {
  data () {
    return {
      rolesList: [],
      isShowAssignRight: false,
      assignRightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      roleId: -1
    }
  },
  created () {
    this.renderList()
    this.getAllRightsList()
  },
  methods: {
    async renderList () {
      const res = await this.$http.get('roles')
      // console.log(res.data.data[0].children[0].authName)
      this.rolesList = res.data.data
    },
    showAssignRight (curUser) {
      this.isShowAssignRight = true
      console.log(curUser)
      this.roleId = curUser.id
      // 展示用户拥有的权限选中状态
      // this.$refs.tree.setCheckedKeys([105])
      // console.log(this.$refs, this.$refs.tree)
      this.$nextTick(() => {
        // 只要叶子节点选中，父节点都会选中
        const checkedKeys = []
        curUser.children.forEach(level1 => {
          level1.children.forEach(level2 => {
            level2.children.forEach(level3 => {
              // 收集三级所有节点id值
              checkedKeys.push(level3.id)
            })
          })
        })
        this.$refs.tree.setCheckedKeys(checkedKeys)
      })
    },
    // 不论是怎样的权限选中，都需要全部数据的显示
    async getAllRightsList () {
      const res = await this.$http.get(`rights/tree`)
      console.log(res)
      this.assignRightsList = res.data.data
    },
    async assignRights () {
      const CheckedKeys = this.$refs.tree.getCheckedKeys()
      const HalfCheckedKeys = this.$refs.tree.getHalfCheckedKeys()
      // const getAllkeys = getCheckedKeys.concat(getHalfCheckedKeys)

      const Allkeys = [...CheckedKeys, ...HalfCheckedKeys]
      console.log(Allkeys)
      const res = await this.$http.post(`roles/${this.roleId}/rights`, {
        rids: Allkeys.join(',')
      })
      console.log(res)
      this.isShowAssignRight = false
      this.renderList()
    }
  }
}
